package com.ttms.service.impl;

import com.ttms.dao.SeatDao;
import com.ttms.dao.StudioDao;
import com.ttms.dao.impl.SeatDaoImpl;
import com.ttms.dao.impl.StudioDaoImpl;
import com.ttms.pojo.Page;
import com.ttms.pojo.Seat;
import com.ttms.pojo.Studio;
import com.ttms.service.StudioService;
import java.util.List;

public class StudioServiceImpl implements StudioService {
    private StudioDao studioDao = new StudioDaoImpl();
    private SeatDao seatDao = new SeatDaoImpl();

    @Override
    public void addStudio(Studio studio) {
        studioDao.addStudio(studio);
    }

    @Override
    public void deleteStudioById(Integer id) {
        studioDao.deleteStudioById(id);
    }

    @Override
    public void updateStudio(Studio studio) {
        studioDao.updateStudio(studio);
    }

    @Override
    public Studio queryStudioById(Integer id) {
        return studioDao.queryStudioById(id);
    }

    @Override
    public List<Studio> queryStudios() {
        return studioDao.queryStudios();
    }

    @Override
    public Page<Studio> page(int pageNo, int pageSize) {
        Page<Studio> page = new Page<Studio>();
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = studioDao.queryForPageTotalCount();
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);
        // 设置当前页码
        page.setPageNo(pageNo);
        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        // 求当前页数据
        List<Studio> items = studioDao.queryForPageItems(begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Page<Studio> queryStudioByName(int pageNo, int pageSize, String studio_name) {
        Page<Studio> page = new Page<Studio>();
        // 设置每页显示的数量
        page.setPageSize(pageSize);
        // 求总记录数
        Integer pageTotalCount = studioDao.queryStudioCountByName(studio_name);
        // 设置总记录数
        page.setPageTotalCount(pageTotalCount);
        // 求总页码
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        // 设置总页码
        page.setPageTotal(pageTotal);
        // 设置当前页码
        page.setPageNo(pageNo);
        // 求当前页数据的开始索引
        int begin = (page.getPageNo() - 1) * pageSize;
        // 求当前页数据
        List<Studio> items = studioDao.queryStudioByName(pageNo, pageSize, studio_name);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public void addSeat(int studio_id) {
        Studio studio = studioDao.queryStudioById(studio_id);
        for (Integer i = 1; i <= studio.getStudio_row_count();i++) {
            for (Integer j = 1; j <= studio.getStudio_col_count(); j++) {
                Seat seat = new Seat();
                seat.setSeat_id(null);
                seat.setSeat_row(i);
                seat.setSeat_column(j);
                seat.setSeat_status(1);
                seat.setStudio_id(studio_id);
                seatDao.addSeat(seat);
            }
        }
    }

    @Override
    public List<Studio> findStudios() {
        return studioDao.queryStudioByFlag();
    }
}
